package com.song.service.Impl;

import com.song.controller.admin.EmployeeController;
import com.song.dao.EmployeeMapper;
import com.song.pojo.Employee;
import com.song.service.EmployeeService;
import com.song.utils.PasswordUtil;
import com.song.utils.SystemConstant;
import com.song.utils.UUIDUtils;
import com.song.vo.EmployeeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 宋超
 * HP - the login name of the current user.
 * 2020/12/5 - the current system date.
 * 17:10 - the current system time.
 */
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    //员工登录
    public Employee Login(String loginName, String loginPwd) {
        //System.out.println("service层用户名："+loginName);
        //通过登录账号查询是否存在此员工
        Employee employee = employeeMapper.findEmployeeByLoginName(loginName);
        //判断对象是否为空
        if (employee!=null){
            //进行加密操作
            String newPassword = PasswordUtil.md5(loginPwd,employee.getSalt(),SystemConstant.PASSWORD_COUNT);
            System.out.println(newPassword);
            //比较密码是否一致
            if(employee.getLoginPwd().equals(newPassword)){
                return employee;//登录成功
            }
        }
        return null;//登录失败
    }

    //根据部门编号查询员工
    public int getEmployeeCountById(Integer deptId) {
        int countById = employeeMapper.getEmployeeCountById(deptId);
        return countById;
    }


    //根据角色编号查询员工数量
    public int getEmployeeCountByRoleId(Integer roleId) {
        return employeeMapper.getEmployeeCountByRoleId(roleId);
    }


    //查询员工列表
    public List<Employee> findEmployeeList(EmployeeVo employeeVo) {
        return employeeMapper.findEmployeeList(employeeVo);
    }

    //添加员工
    public int addEmployee(Employee employee) {
        employee.setSalt(UUIDUtils.randomUUID());//加密盐值
        employee.setCreateDate(new Date());//创建时间
        employee.setLoginPwd(PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD, employee.getSalt(),
                SystemConstant.PASSWORD_COUNT));//密码
        return employeeMapper.addEmployee(employee);
    }


    //修改员工
    public int updateEmployee(Employee employee) {
        //设置修改时间
        employee.setModifyDate(new Date());
        return employeeMapper.updateEmployee(employee);
    }


    //删除员工信息
    public int deleteById(Integer id) {
        //删除员工角色关系表的数据
        employeeMapper.deleteEmployeeAndRole(id);
        //调用删除员工的方法
        return employeeMapper.deleteById(id);
    }

    //重置密码
    public int resetPwd(int id) {
        Employee employee = new Employee();
        employee.setSalt(UUIDUtils.randomUUID());//设置盐
        employee.setLoginPwd(PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD, employee.getSalt(),
                SystemConstant.PASSWORD_COUNT));//重置密码 设置密码
        employee.setId(id);//重置那个员工的密码 id
        return employeeMapper.updateEmployee(employee);
    }

    //保存员工与角色之间的关系
    public boolean saveEmployeeRole(String roleIds, Integer empId) {
        try{
            //首先根据员工id删除原来分配的角色
            employeeMapper.deleteEmployeeAndRole(empId);
            //再保存员工角色关系
            String [] idStr = roleIds.split(",");
            for (int i = 0; i < idStr.length; i++) {
                employeeMapper.addEmployeeRole(idStr[i],empId);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
